"""
@Author  : 康帅
@Time    : 2021/3/25 16:24
@Function: 批量插入sql，超快速度
"""
import time

import MySQLdb

conn = MySQLdb.connect(host='localhost', port=3306, user='root', passwd='1v7h3qw')
curs = conn.cursor()
conn.select_db('ks')
print("数据库连接成功")


def timer(func):
    def decor(*args):
        start_time = time.time()
        func(*args)
        end_time = time.time()
        d_time = end_time - start_time
        print("运行时间: ", d_time)

    return decor


@timer
def insert_sql():
    users_values = []
    # 注意要用两个括号扩起来
    for num in range(1, 2001):
        users_values.append(('%s' % num, 0, '2021-03-25 15:00:29.000000', '2021-03-25 15:00:34.000000', '2w_%sst' % num,
                             '2w_%sst' % num, 4, 0, 1, '2021-03-25 15:03:52.000000', '2021-03-25 15:03:54.000000', 1, 1,
                             '2w_%sst' % num, 20000, 1, 0))

    curs.executemany(
        'insert into `project_model` (id, is_delete, updated_at, created_at, name, '
        'data_file, state, number, tool, release_time, finish_time, is_function, '
        'is_clean_empty, file_path, file_total, builder_id, is_invisible) '
        'values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)', users_values)

    conn.commit()
    curs.close()
    conn.close()
    print('数据插入完成')


if __name__ == '__main__':
    insert_sql()
